Attribute VB_Name = "modReqPacket"
Option Explicit

'Request Message Decode Description With PID1_PID2 Enumeration
Public Enum RequestPacketPidEnum
    pidRequestStatusPacket = &H101&
    pidRequestSpectrum = &H201&
    pidRequestAndClearSpectrum = &H202&
    pidRequestSpectrumAndStatus = &H203&
    pidRequestAndClearSpectrumAndStatus = &H204&
    pidRequestDigitalScopeData = &H301&
    pidRequest512ByteMiscData = &H302&
    pidRequestDigitalScopeDataAndReArmScope = &H303&
    pidRequestEthernetSettings = &H304&
    pidRequestDiagnosticData = &H305&
    pidRequestNetFinderPacket = &H307&
    pidPerformI2CTransfer = &H308&
    pidRequestListModeData = &H309&
    pidRequestOptionPACalibrationData = &H30A&
    
    pidRequestMiniX2TubeInterlockTable = &H30B&
    pidRequestMiniX2WarmupTable = &H30C&
    pidRequestMiniX2TimestampRecord = &H30D&
    pidRequestMiniX2FaultRecord = &H30E&
    
    pidRequest32BitSCACounters = &H401&
    pidLatchAndRequest32BitSCACounters = &H402&
    pidLatchAndClearAndRequest32BitSCACounters = &H403&
    pidTextConfiguration_ToDP5 = &H2002&
    pidTextConfigurationReadback_FromDP5 = &H2003&
    pidEraseFPGAImage = &H3001&
    pidUploadPacketFPGA = &H3002&
    pidReinitializeFPGA = &H3003&
    pidEraseuCImage_1 = &H3005&
    pidUploadPacketuC = &H3007&
    pidSwitchTouCImage_1 = &H3009&
    pidUploadPacket_FPGA_FPGAACK = &H300B&
    pidClearSpectrumBuffer = &HF001&
    pidEnableMCA_MCS = &HF002&
    pidDisableMCA_MCS = &HF003&
    pidArmDigitalOscilloscope = &HF004&
    pidAutosetInputOffset = &HF005&
    pidAutosetFastThreshold = &HF006&
    pidReadIO3_0 = &HF007&
    pidWriteIO3_0 = &HF008&
    pidWrite512ByteMiscData = &HF009&
    pidSetDCAL = &HF00A&
    pidSetPZCorrection = &HF00B&
    pidSetuCTempCal = &HF00C&
    pidSetADCCal_Gain_Offset = &HF00E&
    pidClearGPCounter = &HF010&
    pidSetEthernetSettings = &HF011&
    pidSelectHigh_PassTimeConstant = &HF012&
    pidSelectRS232BaudRate = &HF013&
    pidSetHVCal_Gain_Offset = &HF014&
    pidSet1_6uSPZCorrection = &HF015&
    pidClear_SyncListModeTimer = &HF016&
    pidSetZeroOffset_1VScale = &HF019&
    pidSetZeroOffset_10VScale = &HF01A&
    pidInterfaceKeep_Alive_AllowSharing = &HF020&
    pidInterfaceKeep_Alive_NoSharing = &HF021&
    pidInterfaceKeep_Alive_Lock = &HF022&
    
    pidWriteMiniX2TubeInterlockTable = &HF030&
    pidWriteMiniX2Timestamp = &HF031&
    pidWriteMiniX2WarmupSequence = &HF032&
    pidInitiateMiniX2WarmupSequence = &HF033&
    
    pidCommTest_RequestACKPacket = &HF100&
    pidCommTest_RequestACKPacketMax = &HF10F&
    pidCommTest_StreamingTestMode = &HF17E&
    pidCommTest_EchoPacket = &HF17F&
End Enum

Public Function DecodeRequestPacket(PIN As Packet_In, Optional bIncData As Boolean = True) As String
    Dim strReqPkt As String
    Dim strData As String
    Dim lPid1Pid2 As Long
    strReqPkt = ""
    strData = ""
    lPid1Pid2 = 0
    lPid1Pid2 = (CLng(PIN.PID1) * &H100&) + CLng(PIN.PID2)
    Select Case lPid1Pid2   'Request Packet Description By PID(1&2)
        Case pidRequestStatusPacket
            strReqPkt = "Request Status Packet"
        Case pidRequestSpectrum
            strReqPkt = "Request Spectrum"
        Case pidRequestAndClearSpectrum
            strReqPkt = "Request & Clear Spectrum"
        Case pidRequestSpectrumAndStatus
            strReqPkt = "Request Spectrum + Status"
        Case pidRequestAndClearSpectrumAndStatus
            strReqPkt = "Request & Clear Spectrum + Status"
        Case pidRequestDigitalScopeData
            strReqPkt = "Request Digital Scope Data"
        Case pidRequest512ByteMiscData
            strReqPkt = "Request 512-Byte Misc Data"
        Case pidRequestDigitalScopeDataAndReArmScope
            strReqPkt = "Request Digital Scope Data & Re-arm Scope"
        Case pidRequestEthernetSettings
            strReqPkt = "Request Ethernet Settings"
        Case pidRequestDiagnosticData
            strReqPkt = "Request Diagnostic Data"
        Case pidRequestNetFinderPacket
            strReqPkt = "Request NetFinder Packet"
        Case pidPerformI2CTransfer
            strReqPkt = "Perform I2C Transfer"
        Case pidRequestListModeData
            strReqPkt = "Request List-Mode Data"
        Case pidRequestOptionPACalibrationData
            strReqPkt = "Request Option PA Calibration Data"
            
        '----------------------------------------------------------- Mini-X2
        Case pidRequestMiniX2TubeInterlockTable                     '= &H30B&
            strReqPkt = "Request Mini-X2 Tube and Interlock Table"
        Case pidRequestMiniX2WarmupTable                            ' = &H30C&
            strReqPkt = "Request Mini-X2 Warmup Table"
        Case pidRequestMiniX2TimestampRecord                        '= &H30D&
            strReqPkt = "Request Mini-X2 Timestamp Record"
        Case pidRequestMiniX2FaultRecord                            '= &H30E&
            strReqPkt = "Request Mini-X2 Fault Record"
        '----------------------------------------------------------- Mini-X2
            
        Case pidRequest32BitSCACounters
            strReqPkt = "Request 32-bit SCA Counters"
        Case pidLatchAndRequest32BitSCACounters
            strReqPkt = "Latch + Request 32-bit SCA Counters"
        Case pidLatchAndClearAndRequest32BitSCACounters
            strReqPkt = "Latch + Clear + Request 32-Bit SCA Counters"
        Case pidTextConfiguration_ToDP5
            strReqPkt = "Text Configuration (to DP5)"
            If (bIncData) Then strData = " " & CfgBytesToString(PIN)
        Case pidTextConfigurationReadback_FromDP5
            strReqPkt = "Text Configuration Readback (from DP5)"
            If (bIncData) Then strData = " " & CfgBytesToString(PIN)
        Case pidEraseFPGAImage
            strReqPkt = "Erase FPGA Image"
        Case pidUploadPacketFPGA
            strReqPkt = "Upload Packet (FPGA)"
        Case pidReinitializeFPGA
            strReqPkt = "Reinitialize FPGA"
        Case pidEraseuCImage_1
            strReqPkt = "Erase uC Image #1"
        Case pidUploadPacketuC
            strReqPkt = "Upload Packet (uC)"
        Case pidSwitchTouCImage_1
            strReqPkt = "Switch To uC Image #1"
        Case pidUploadPacket_FPGA_FPGAACK
            strReqPkt = "Upload Packet (FPGA); FPGA ACK"
        Case pidClearSpectrumBuffer
            strReqPkt = "Clear Spectrum Buffer"
        Case pidEnableMCA_MCS
            strReqPkt = "Enable MCA/MCS"
        Case pidDisableMCA_MCS
            strReqPkt = "Disable MCA/MCS"
        Case pidArmDigitalOscilloscope
            strReqPkt = "Arm Digital Oscilloscope"
        Case pidAutosetInputOffset
            strReqPkt = "Autoset Input Offset"
        Case pidAutosetFastThreshold
            strReqPkt = "Autoset Fast Threshold"
        Case pidReadIO3_0
            strReqPkt = "Read IO3-0"
        Case pidWriteIO3_0
            strReqPkt = "Write IO3-0"
        Case pidWrite512ByteMiscData
            strReqPkt = "Write 512-Byte Misc Data"
        Case pidSetDCAL
            strReqPkt = "Set DCAL"
        Case pidSetPZCorrection
            strReqPkt = "Set PZ Correction"
        Case pidSetuCTempCal
            strReqPkt = "Set uC Temp Cal"
        Case pidSetADCCal_Gain_Offset
            strReqPkt = "Set ADC Cal (Gain/Offset)"
        Case pidClearGPCounter
            strReqPkt = "Clear G.P. Counter"
        Case pidSetEthernetSettings
            strReqPkt = "Set Ethernet Settings"
        Case pidSelectHigh_PassTimeConstant
            strReqPkt = "Select High-Pass Time Constant"
        Case pidSelectRS232BaudRate
            strReqPkt = "Select RS232 Baud Rate"
        Case pidSetHVCal_Gain_Offset
            strReqPkt = "Set HV Cal (Gain/Offset)"
        Case pidSet1_6uSPZCorrection
            strReqPkt = "Set 1.6uS PZ Correction"
        Case pidClear_SyncListModeTimer
            strReqPkt = "Clear/Sync List-Mode Timer"
        Case pidSetZeroOffset_1VScale
            strReqPkt = "Set Zero Offset; 1V Scale"
        Case pidSetZeroOffset_10VScale
            strReqPkt = "Set Zero Offset; 10V Scale"
        Case pidInterfaceKeep_Alive_AllowSharing
            strReqPkt = "Interface Keep-alive - allow Sharing"
        Case pidInterfaceKeep_Alive_NoSharing
            strReqPkt = "Interface Keep-alive - no Sharing"
        Case pidInterfaceKeep_Alive_Lock
            strReqPkt = "Interface Keep-alive - lock"
            
        '----------------------------------------------------------- Mini-X2
        Case pidWriteMiniX2TubeInterlockTable                       '= &HF030&
            strReqPkt = "Write Mini-X2 Tube and Interlock Table"
        Case pidWriteMiniX2Timestamp                                '= &HF031&
            strReqPkt = "Write Mini-X2 Timestamp"
        Case pidWriteMiniX2WarmupSequence                           '= &HF032&
            strReqPkt = "Write Mini-X2 Warmup Sequence"
        Case pidInitiateMiniX2WarmupSequence                        '= &HF033&
            strReqPkt = "Initiate Mini-X2 Warmup Sequence"
        '----------------------------------------------------------- Mini-X2
                
        Case pidCommTest_RequestACKPacket To pidCommTest_RequestACKPacketMax
            strReqPkt = "Comm Test - Request ACK Packet"
            If (bIncData) Then strData = " " & CStr(PIN.PID2)
        Case pidCommTest_StreamingTestMode
            strReqPkt = "Comm Test - Streaming Test Mode"
        Case pidCommTest_EchoPacket
            strReqPkt = "Comm Test - Echo Packet"
        End Select
        DecodeRequestPacket = strReqPkt & strData
End Function

Public Function CfgBytesToString(PIN As Packet_In) As String
    Dim idxCfg As Long
    Dim strCfg As String
    strCfg = ""
    For idxCfg = 0 To PIN.LEN - 1
        strCfg = strCfg + Chr(PIN.Data(idxCfg))
    Next
    CfgBytesToString = strCfg
End Function


